package com;

import com.xnx3.j2ee.util.ConsoleUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 运行入口
 *
 */
@EnableTransactionManagement
@SpringBootApplication
@ServletComponentScan
public class Application extends SpringBootServletInitializer {
	
	// 运行时的参数
	private static String[] args;

	// spring容器的配合容器组件
	public static ConfigurableApplicationContext context;

	public static void main(String[] args) {
		ConsoleUtil.info("Is run at  here?");
		context = SpringApplication.run(Application.class, args);
		Application.args = args;
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		ConsoleUtil.debug = true;
		ConsoleUtil.info = true;
		ConsoleUtil.error = true;
		com.xnx3.j2ee.Global.isJarRun = false;	//自行放到tomcat中运行

		application.allowCircularReferences(true);
		return application.sources(Application.class);
	}

	/**
	 * 进行Spring相关的信息进行重新加载
	 * @author 李鑫
	 */
	public static void restart(){
		ExecutorService threadPool = new ThreadPoolExecutor (1,1,0, TimeUnit.SECONDS,new ArrayBlockingQueue<> ( 1 ),new ThreadPoolExecutor.DiscardOldestPolicy ());
		threadPool.execute (()->{
			Application.context.close();
			Application.context = SpringApplication.run (Application.class, args);
		} );
		threadPool.shutdown ();
	}


}
